package com.example.tianling.admin.service.Impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tianling.admin.service.GoodService;
import com.example.tianling.common.utils.MinioUtils;
import com.example.tianling.dao.ProductMapper;
import com.example.tianling.entity.ProductEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import static com.example.tianling.common.utils.ImageUtils.compress;
import static com.example.tianling.common.utils.toImg.convert;
import static com.example.tianling.common.utils.toImg.getImageFormat;

@Service
public class GoodServiceImpl
        extends ServiceImpl<ProductMapper, ProductEntity>
        implements GoodService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private MinioUtils minioUtils;
//插入新的商品
   public Integer uploadGood(ProductEntity productEntity){
       JSONObject res = new JSONObject();
       try {
           BufferedImage image = convert(productEntity.getMainImage());
           BufferedImage compressedImage = compress(image); // Compress the image using your compress method
           String fileExtension = getImageFormat(productEntity.getMainImage());
           res = minioUtils.uploadBufferedImage(compressedImage, "commodity", fileExtension); // Upload the compressed image
       } catch (Exception e) {
           e.printStackTrace();
           res.put("code", 0);
           res.put("msg", "上传失败");
       }
       productEntity.setMainImage(res.get("msg").toString());
       return productMapper.insert(productEntity);
   }
   @Override
    public List getGood(){
//       QueryWrapper<ProductEntity> queryWrapper = new QueryWrapper<>();
       System.out.println(productMapper.selectList(null));
       List<ProductEntity> list = productMapper.selectList(null);
       return list;
   }
   @Override
    public ProductEntity getByProId(String proId){
       return productMapper.selectById(proId);
   }

   @Override
    public Integer updataGood(ProductEntity productEntity){
       LocalDateTime now = LocalDateTime.now();
       // 定义时间格式
       DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
       // 格式化时间
       String formattedDateTime = now.format(formatter);
       JSONObject res = new JSONObject();
       try {

           BufferedImage image = convert(productEntity.getMainImage());
           BufferedImage compressedImage = compress(image); // Compress the image using your compress method
           String fileExtension = getImageFormat(productEntity.getMainImage());
           res = minioUtils.uploadBufferedImage(compressedImage, "commodity", fileExtension); // Upload the compressed image
       } catch (Exception e) {
           e.printStackTrace();
           res.put("code", 0);
           res.put("msg", "上传失败");
       }
       productEntity.setMainImage(res.get("msg").toString());
       productEntity.setUpdateTime(formattedDateTime);
       UpdateWrapper<ProductEntity> productEntityUpdateWrapper = new UpdateWrapper<>();
       productEntityUpdateWrapper.eq("pro_id",productEntity.getProId());
       return productMapper.update(productEntity,productEntityUpdateWrapper);
   }
}
